import { defineStore } from "pinia";
export const useRoleStore = defineStore("role", {
  state: () => {
    return {
      token: null,
      type: 1,
      userInfo: {},
    };
  },
  getters: {
    // 用户等级
    userLevel: state => {
      return state.userInfo.talentIdentificationLevel;
    },
    userName: state => {
      return state.userInfo.userName;
    },
    idnum: state => {
      return state.userInfo.idnum;
    },
  },
  actions: {
    setToken(val) {
      localStorage.setItem("USER_TOKEN", val);
      this.token = val;
    },
    setUserInfo(val) {
      localStorage.setItem("SAVE_USER_INFO", JSON.stringify(val));
      this.userInfo = val;
    },
    handleRefresh() {
      const userInfo = localStorage.getItem("SAVE_USER_INFO");
      if (!!userInfo) {
        this.userInfo = JSON.parse(userInfo);
      }
      const token = localStorage.getItem("USER_TOKEN");
      if (!!token) {
        this.token = token;
      }
    },
  },
});
